package com.qst.survey.controller;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.qst.survey.entity.QuestionTopic;
import com.qst.survey.pojo.PageResult;
import com.qst.survey.pojo.Result;
import com.qst.survey.pojo.StatusCode;
import com.qst.survey.service.QuestionTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Date: 2021/3/18 20:27
 * @Description: 题目控制类
 */

@RequestMapping("/topic")
@RestController
@CrossOrigin
public class TopicController {
    @Autowired
    QuestionTopicService questionTopicService;
    /**
     * @param questionTopic
     * @return com.qst.survey.pojo.Result
     * @description: TODO 添加 题目
     * @date 15:51:47 2021/3/19
     */
    @PostMapping(value = "/addTopic")
    public Result addTopic(@RequestBody QuestionTopic questionTopic) {
        String replace = SensitiveWordBs.newInstance().replace(questionTopic.getTitle());
        questionTopic.setTitle(replace);
        Integer integer = questionTopicService.addTopic(questionTopic);
        if (integer == 0) {
            return new Result(false, StatusCode.ERROR, "添加失败!");
        } else {
            return new Result(true, StatusCode.OK, "添加成功!");

        }
    }
    /**
     * @param questionId: 投票id
     * @return com.qst.survey.pojo.Result
     * @description: TODO 根据试卷id查找所有的题目
     * @date 16:59:18 2021/3/22
     */
    @PostMapping(value = "/findAll")
    public Result findAll(String questionId) {
        return new Result(true, StatusCode.OK, "查询成功!");
    }

    /**
     * @param id 题目id
     * @description  TODO 通过题目id来删除题目
     * @return com.qst.survey.pojo.Result
     * @date 2021/4/2
    */
    @DeleteMapping("/delete/{id}")
    public Result deleteOneTopic(@PathVariable Integer id) {
        Integer integer = questionTopicService.deleteTopic(id);
        if (integer == 1) {
            return new Result(true, StatusCode.OK, "删除成功!");
        } else {
            return new Result(false, StatusCode.ERROR, "删除失败!");

        }
    }
    /**
     * @param questionTopic  题目
     * @description  TODO   更新题目
     * @return com.qst.survey.pojo.Result
     * @date 2021/4/2
    */
    @PostMapping("/updateTopic")
    public Result updateTopic(@RequestBody QuestionTopic questionTopic) {
        Integer integer = questionTopicService.updateTopic(questionTopic);
        if (integer == 1) {
            return new Result(true, StatusCode.OK, "修改成功!");
        } else {
            return new Result(false, StatusCode.ERROR, "修改失败!");
        }
    }
    /**
     * @param id 题目id
     * @description  TODO 查出所有题目
     * @return com.qst.survey.pojo.Result
     * @date 2021/4/2
    */
    @PostMapping("/findById/{id}")
    public Result findById(@PathVariable int id){
        List<QuestionTopic> questionTopicList=questionTopicService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", questionTopicList);
    }

}
